package com.wgts.wgts.mvp.view.activity.wgtshome;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wgts.wgts.R;
import com.wgts.wgts.control.recycleview.BaseLoadMoreAdapter;
import com.wgts.wgts.netmodle.resp.ArticleEntity;
import com.wgts.wgts.tools.MoHuGlide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WgtsHomePageAdapter extends BaseLoadMoreAdapter<RecyclerView.ViewHolder> {


    public static final int HEAD = 0x0001;

    public static final int BODY = 0xFFFF;


    private Context context;

    private List<ArticleEntity> articleEntities;

    private OnClickListener onClickListener;

    public WgtsHomePageAdapter(Context context, List<ArticleEntity> articleEntities) {
        super(context);
        this.context = context;
        this.articleEntities = articleEntities;

    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateBodyViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == HEAD) {
            return new HeadViewHolder(LayoutInflater.from(context).inflate(R.layout.wgts_home_page_view_holder_head, viewGroup, false));
        }

        return new BodyViewHolder(LayoutInflater.from(context).inflate(R.layout.wgts_home_page_view_holder, viewGroup, false));
    }

    @Override
    public void onBindBodyViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        if (viewHolder instanceof HeadViewHolder) {

            //CustomImageView.getInstance().customImage(context, ((HeadViewHolder) viewHolder).imgHead, 211/(double)343);
            MoHuGlide.getInstance().glideImgsFormListRaduice(((HeadViewHolder) viewHolder).imgHead, articleEntities.get(i).bigCover, context);
            ((HeadViewHolder) viewHolder).tvDetail.setText(articleEntities.get(i).title);

            ((HeadViewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClick(v, i);
                }
            });
        }
        if (viewHolder instanceof BodyViewHolder) {
            MoHuGlide.getInstance().glideImgsFormListRaduice(((BodyViewHolder) viewHolder).imgLeft, articleEntities.get(i).cover, context);
            ((BodyViewHolder) viewHolder).tvDetail.setText(articleEntities.get(i).title);
            ((BodyViewHolder) viewHolder).tvAuthr.setText(articleEntities.get(i).author);
            ((BodyViewHolder) viewHolder).tvTime.setText(articleEntities.get(i).publishTime);

            ((BodyViewHolder) viewHolder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClick(v, i);
                }
            });
        }
    }


    @Override
    public int getBodyItemCount() {
        return articleEntities.size();
    }

    @Override
    public int getBodyItemViewType(int position) {
        if (position == 0) {
            return HEAD;
        }
        return BODY;
    }


    public class HeadViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_head)
        ImageView imgHead;
        @BindView(R.id.tv_detail)
        TextView tvDetail;

        View itemView;

        public HeadViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }


    public class BodyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.img_left)
        ImageView imgLeft;
        @BindView(R.id.tv_detail)
        TextView tvDetail;
        @BindView(R.id.tv_authr)
        TextView tvAuthr;
        @BindView(R.id.tv_time)
        TextView tvTime;

        View itemView;

        public BodyViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.itemView = itemView;
        }
    }

    interface OnClickListener {
        void onClick(View view, int i);
    }

}
